﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CSDL_NC_07.BUS;
using CSDL_NC_07.DAO;
using System.Data.SqlClient;
using System.Globalization;

namespace CSDL_NC_07.GUI.other
{
    public partial class MHThemHDXV : UserControl
    {
        public THUNGAN thuNgan { get; set; }
        private decimal outputValue = 0;

        public MHThemHDXV()
        {
            InitializeComponent();
        }

        private void MHThemHDXV_Load(object sender, EventArgs e)
        {
            txtMaBenhNhan.Focus();
        }


        #region btnKiemTra_Click()
        private void btnKiemTra_Click(object sender, EventArgs e)
        {
            if (txtMaBenhNhan.Text.Length < 12 || txtMaBenhNhan.Text.Length > 12)
            {
                MessageBox.Show("Mã bệnh nhân không hợp lệ.");
                return;
            }
                

            outputValue = 0;
            DataProvider dataProvider = new DataProvider();
           
            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("MaBenhNhan", txtMaBenhNhan.Text));
            parameterList.Add(new SqlParameter("SoTienHoaDonXuatVien", outputValue));
            parameterList[parameterList.Count - 1].SqlDbType = SqlDbType.Money;
            parameterList[parameterList.Count - 1].Direction = ParameterDirection.InputOutput;
            parameterList.Add(new SqlParameter("ThuNgan_ID", null));

            if (!dataProvider.IsConnect())
                dataProvider.Connect();

            DataSet ds = dataProvider.ExcuteProcedureGetDataSetWithOutput("usp_LapHoaDonXuatVien", parameterList.ToArray(), out outputValue);
            dataProvider.Disconnect();

            if (outputValue > 0)
            {
                gViewTamUng.DataSource = ds.Tables[0];
                gViewThuoc.DataSource = ds.Tables[1];
                gViewPhong.DataSource = ds.Tables[2];
                gViewDungCu.DataSource = ds.Tables[3];
                gViewDichVu.DataSource = ds.Tables[4];
                sContainer1.Visible = true;
                panel1.Visible = true;

                // ẩn nút
                label1.Visible = false;
                txtMaBenhNhan.Visible = false;
                btnKiemTra.Visible = false;

                //
                CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");
                txtTienThanhToan.Text = outputValue.ToString("#,###", cul.NumberFormat);
            }
            
        }
        #endregion

        #region btnThanhToan_Click()
        private void btnThanhToan_Click(object sender, EventArgs e)
        {
            DataProvider dataProvider = new DataProvider();

            List<SqlParameter> parameterList = new List<SqlParameter>();
            parameterList.Add(new SqlParameter("MaBenhNhan", txtMaBenhNhan.Text));
            parameterList.Add(new SqlParameter("SoTienHoaDonXuatVien", outputValue));
            parameterList[parameterList.Count - 1].SqlDbType = SqlDbType.Money;
            parameterList[parameterList.Count - 1].Direction = ParameterDirection.InputOutput;
            parameterList.Add(new SqlParameter("ThuNgan_ID", thuNgan.ThuNgan_ID));

            int num = -1;
            if (!dataProvider.IsConnect())
                dataProvider.Connect();

            num = dataProvider.ExcuteProcedureWithReturn("usp_LapHoaDonXuatVien", parameterList.ToArray());
            //DataSet ds = dataProvider.ExcuteProcedureGetDataSetWithOutput("usp_LapHoaDonXuatVien", parameterList.ToArray(), out outputValue);
            dataProvider.Disconnect();

            if (num != 1)
            {
                MessageBox.Show("có lỗi xảy ra.");
            }
            else
            {
                MessageBox.Show("Lập hóa đơn xuất viện thành công.");
                OnLapPhieuXuatVien();
            }
        }
        #endregion

        public event EventHandler LapPhieuXuatVienThanhCong;
        private void OnLapPhieuXuatVien()
        {
            EventHandler handler = LapPhieuXuatVienThanhCong;
            if (handler != null) handler(this, EventArgs.Empty);
        }

    }
}
